//
//  IMConversation.m
//  MLIM
//
//  Created by molon on 15/6/24.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "IMConversation.h"
#import "IMCommon.h"

@implementation IMConversation

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        self.conversationId = [coder decodeObjectForKey:SELSTR(conversationId)];
        self.unreadCount = [[coder decodeObjectForKey:SELSTR(unreadCount)] integerValue];
        self.lastMessage = [coder decodeObjectForKey:SELSTR(lastMessage)];
        self.avatarURL = [coder decodeObjectForKey:SELSTR(avatarURL)];
        self.name = [coder decodeObjectForKey:SELSTR(name)];
        self.clientIds = [coder decodeObjectForKey:SELSTR(clientIds)];
        self.lastMessageTime = [[coder decodeObjectForKey:SELSTR(lastMessageTime)] doubleValue];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.conversationId forKey:SELSTR(conversationId)];
    [aCoder encodeObject:@(self.unreadCount) forKey:SELSTR(unreadCount)];
    [aCoder encodeObject:self.lastMessage forKey:SELSTR(lastMessage)];
    [aCoder encodeObject:self.avatarURL forKey:SELSTR(avatarURL)];
    [aCoder encodeObject:self.name forKey:SELSTR(name)];
    [aCoder encodeObject:self.clientIds forKey:SELSTR(clientIds)];
    [aCoder encodeObject:@(self.lastMessageTime) forKey:SELSTR(lastMessageTime)];
}

- (BOOL)isValidToDisplay
{
    return (self.conversationId.length>0&&self.lastMessageTime>0);
}

- (BOOL)isHaveDetail
{
    return (self.name.length>0&&self.avatarURL);
}

- (double)lastMessageTime
{
    if (self.lastMessage) {
        return self.lastMessage.sendTimestamp;
    }
    return _lastMessageTime;
}
@end
